﻿
'*******************************************************************
' Program: Data Center Visio Rack Tool
' Author: Albert E Edlund
' Date:
'
' Purpose:
'
'*******************************************************************




Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging

Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Collections.ObjectModel

Imports System.Data


Imports visRackLocations.clsDC_RackData


Imports System.Xml


Public Class clsAttrCabinetRows

#Region " Properties and definitions "

    Public m_db As Database
    Public m_ds As DataSet
    Public m_dt As DataTable

    Public m_dc As dc_CabinetRow
    Public m_oc As Collection(Of dc_CabinetRow)


    Private strUserId As String

    Public m_deleteCommand As DbCommand
    Public m_insertCommand As DbCommand
    Public m_updateCommand As DbCommand

    Public Const readAll As String = "selecttblAttrCabinetRow"

    ' who am I
    Private myEnum As DC_RackTables = DC_RackTables.tblAttrCabinetRow

#End Region

#Region " Move/Transform Data "

    '
    ' create columns
    '
    Public Sub CreateTableColumns(ByRef tempTable As DataTable)

        Dim newDC As dc_CabinetRow = New dc_CabinetRow(strUserId)

        Try

            Dim colRowKey As New System.Data.DataColumn
            colRowKey.ColumnName = "rowKey"
            colRowKey.DataType = GetType(Integer)
            colRowKey.DefaultValue = m_dc.rowKey
            tempTable.Columns.Add(colRowKey)

            '
            ' concatenation of rowname and side
            '
            Dim colRowId As New System.Data.DataColumn
            colRowId.ColumnName = "rowId"
            colRowId.DataType = GetType(String)
            colRowId.DefaultValue = m_dc.rowId
            tempTable.Columns.Add(colRowId)

            Dim colRowLoc As New System.Data.DataColumn
            colRowLoc.ColumnName = "rowLoc"
            colRowLoc.DataType = GetType(Int32)
            colRowLoc.DefaultValue = m_dc.rowLoc
            tempTable.Columns.Add(colRowLoc)

            Dim colRowName As New System.Data.DataColumn
            colRowName.ColumnName = "rowName"
            colRowName.DataType = GetType(String)
            colRowName.DefaultValue = m_dc.rowName
            tempTable.Columns.Add(colRowName)

            Dim colRowSide As New System.Data.DataColumn
            colRowSide.ColumnName = "rowSide"
            colRowSide.DataType = GetType(String)
            colRowSide.DefaultValue = m_dc.rowSide
            tempTable.Columns.Add(colRowSide)

            Dim colRowGrid As New System.Data.DataColumn
            colRowGrid.ColumnName = "rowGrid"
            colRowGrid.DataType = GetType(String)
            colRowGrid.DefaultValue = m_dc.rowGrid
            tempTable.Columns.Add(colRowGrid)

            Dim colRowDefaultVendor As New System.Data.DataColumn
            colRowDefaultVendor.ColumnName = "rowDefaultVendor"
            colRowDefaultVendor.DataType = GetType(Integer)
            colRowDefaultVendor.DefaultValue = m_dc.rowDefaultVendor
            tempTable.Columns.Add(colRowDefaultVendor)

            Dim colRowCabCt As New System.Data.DataColumn
            colRowCabCt.ColumnName = "rowCabCt"
            colRowCabCt.DataType = GetType(Integer)
            colRowCabCt.DefaultValue = m_dc.rowCabCt
            tempTable.Columns.Add(colRowCabCt)

            Dim colRowOrientation As New System.Data.DataColumn
            colRowOrientation.ColumnName = "rowOrientation"
            colRowOrientation.DataType = GetType(String)
            colRowOrientation.DefaultValue = m_dc.rowComments
            tempTable.Columns.Add(colRowOrientation)

            Dim colRowComments As New System.Data.DataColumn
            colRowComments.ColumnName = "rowComments"
            colRowComments.DataType = GetType(String)
            colRowComments.DefaultValue = m_dc.rowComments
            tempTable.Columns.Add(colRowComments)


            Dim colRowLastUpdate As New System.Data.DataColumn
            colRowLastUpdate.ColumnName = "rowLastUpdate"
            colRowLastUpdate.DataType = GetType(Date)
            colRowLastUpdate.DefaultValue = m_dc.rowLastUpdate
            tempTable.Columns.Add(colRowLastUpdate)

            Dim colRowLastEdit As New System.Data.DataColumn
            colRowLastEdit.ColumnName = "rowLastEdit"
            colRowLastEdit.DataType = GetType(String)
            colRowLastEdit.DefaultValue = m_dc.rowLastEdit
            tempTable.Columns.Add(colRowLastEdit)

            newDC = Nothing

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        If newDC IsNot Nothing Then
            newDC = Nothing
        End If

    End Sub

    '
    '
    '
    Public Sub movePropertiesToDataRow _
            (ByRef tempRow As DataRow, _
             ByVal tempDC As dc_CabinetRow)

        Try

            With tempDC
                tempRow.SetField("rowKey", .rowKey)
                tempRow.SetField("rowId", .rowId)
                tempRow.SetField("rowLoc", .rowLoc)
                tempRow.SetField("rowName", .rowName)
                tempRow.SetField("rowSide", .rowSide)
                tempRow.SetField("rowGrid", .rowGrid)
                tempRow.SetField("rowDefaultVendor", .rowDefaultVendor)
                tempRow.SetField("rowCabCt", .rowCabCt)
                tempRow.SetField("rowOrientation", .rowOrientation)
                tempRow.SetField("rowComments", .rowComments)
                tempRow.SetField("rowLastUpdate", .rowLastUpdate)
                tempRow.SetField("rowLastEdit", strUserId)
            End With


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Public Sub moveDataRowToProperties _
            (ByVal tempRow As DataRow)

        Try

            With m_dc
                .initProperties()
                .rowKey = tempRow.Item("rowKey")
                .rowId = tempRow.Item("rowId")
                .rowLoc = tempRow.Item("rowLoc")
                .rowName = tempRow.Item("rowName")
                .rowSide = tempRow.Item("rowSide")
                .rowGrid = tempRow.Item("rowGrid")
                .rowDefaultVendor = tempRow.Item("rowDefaultVendor")
                .rowCabCt = tempRow.Item("rowCabCt")
                .rowOrientation = tempRow.Item("rowOrientation")
                .rowComments = tempRow.Item("rowComments")
                .rowLastUpdate = tempRow.Item("rowLastUpdate")
                .rowLastEdit = tempRow.Item("rowLastEdit")
            End With


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


    '
    '
    '
    Public Sub moveDataRowToProperties _
            (ByVal tempRow As DataRow, _
             ByRef tempDC As dc_CabinetRow)

        Try

            With tempDC
                .initProperties()
                .rowKey = tempRow.Item("rowKey")
                .rowId = tempRow.Item("rowId")
                .rowLoc = tempRow.Item("rowLoc")
                .rowName = tempRow.Item("rowName")
                .rowSide = tempRow.Item("rowSide")
                .rowGrid = tempRow.Item("rowGrid")
                .rowDefaultVendor = tempRow.Item("rowDefaultVendor")
                .rowCabCt = tempRow.Item("rowCabCt")
                .rowOrientation = tempRow.Item("rowOrientation")
                .rowComments = tempRow.Item("rowComments")
                .rowLastUpdate = tempRow.Item("rowLastUpdate")
                .rowLastEdit = tempRow.Item("rowLastEdit")
            End With


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


    '
    '
    '
    Public Sub moveDataTableToObservableCollection _
                (ByVal dtTemp As DataTable)

        Try

            If 0 < dtTemp.Rows.Count Then

                If m_oc IsNot Nothing Then
                    m_oc = Nothing
                    m_oc = New Collection(Of dc_CabinetRow)
                Else
                    m_oc = New Collection(Of dc_CabinetRow)
                End If

                Dim drTemp As DataRow
                Dim dcTemp As dc_CabinetRow
                For Each drTemp In dtTemp.Rows
                    dcTemp = New dc_CabinetRow
                    moveDataRowToProperties(drTemp, dcTemp)
                    m_oc.Add(dcTemp)
                Next

            End If

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try



    End Sub

    '
    '
    '
    Public Function moveDataTableToXML() As XmlDocument

        Dim writer = New System.IO.StringWriter
        Dim m_dt_xml As Xml.XmlDocument = Nothing

        Try


            m_dt.WriteXml(writer, XmlWriteMode.WriteSchema, True)
            m_dt_xml = Nothing
            m_dt_xml = New Xml.XmlDocument
            m_dt_xml.LoadXml(writer.ToString)

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Finally

            writer = Nothing

        End Try

        Return m_dt_xml


    End Function

#End Region

#Region " standard calls "

    '
    '
    '
    Public Sub initDataSetAndTable()

        Try

            ' make sure the root stuff is there
            If m_ds Is Nothing Then
                m_ds = New DataSet
            Else
                m_ds = Nothing
                m_ds = New DataSet
            End If
            m_ds.SchemaSerializationMode = SchemaSerializationMode.IncludeSchema

            If m_dt Is Nothing Then
                m_dt = New DataTable
            Else
                m_dt = Nothing
                m_dt = New DataTable
            End If

            If m_oc Is Nothing Then
                m_oc = New Collection(Of dc_CabinetRow)
            Else
                m_oc = Nothing
                m_oc = New Collection(Of dc_CabinetRow)
            End If

            If m_dc Is Nothing Then
                m_dc = New dc_CabinetRow(strUserId)
            Else
                m_dc = Nothing
                m_dc = New dc_CabinetRow(strUserId)
            End If

            CreateTableColumns(m_dt)
            m_dt.TableName = dc_racktables.tblAttrCabinetRow.tostring
            m_ds.Tables.Add(m_dt)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try



    End Sub


    '
    '
    '
    Private Sub buildInsertCabinetRowCommand()

        Try

            m_insertCommand = m_db.GetStoredProcCommand("insertAttrCabinetRow")

            Dim cmdActive As DbCommand
            cmdActive = m_insertCommand

            m_db.AddInParameter(cmdActive, "rowLoc", DbType.Int32, "rowLoc", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "rowId", DbType.String, "rowId", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "rowName", DbType.String, "rowName", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "rowSide", DbType.String, "rowSide", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "rowGrid", DbType.String, "rowGrid", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "rowDefaultVendor", DbType.String, "rowDefaultVendor", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "rowCabCt", DbType.Int16, "rowCabCt", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "rowOrientation", DbType.String, "rowOrientation", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "rowComments", DbType.String, "rowComments", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "rowLastUpdate", DbType.Date, "rowLastUpdate", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "rowLastEdit", DbType.String, "rowLastEdit", DataRowVersion.Current)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try
    End Sub

    '
    '
    '
    Private Sub buildUpdateCabinetRowCommand()



        Try

            m_updateCommand = m_db.GetStoredProcCommand("updateAttrCabinetRow")

            Dim cmdActive As DbCommand
            cmdActive = m_updateCommand

            m_db.AddInParameter(cmdActive, "rowKey", DbType.Int32, "rowKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "rowLoc", DbType.Int32, "rowLoc", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "rowId", DbType.String, "rowId", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "rowName", DbType.String, "rowName", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "rowSide", DbType.String, "rowSide", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "rowGrid", DbType.String, "rowGrid", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "rowDefaultVendor", DbType.String, "rowDefaultVendor", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "rowCabCt", DbType.Int16, "rowCabCt", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "rowOrientation", DbType.String, "rowOrientation", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "rowComments", DbType.String, "rowComments", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "rowLastUpdate", DbType.Date, "rowLastUpdate", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "rowLastEdit", DbType.String, "rowLastEdit", DataRowVersion.Current)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Private Sub buildDeleteCabinetRowCommand()



        Try
            m_deleteCommand = m_db.GetStoredProcCommand("deleteAttrCabinetRow")

            Dim cmdActive As DbCommand
            cmdActive = m_deleteCommand
            m_db.AddInParameter(cmdActive, "rowKey", DbType.Int32, "rowKey", DataRowVersion.Current)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try
    End Sub


    '
    '
    '
    Public Function dbUpdateDataSet() As Int16

        Dim rowsAffected As Int16 = 0

        Try

            rowsAffected = m_db.UpdateDataSet(m_ds, dc_racktables.tblAttrCabinetRow.tostring, _
                                        m_insertCommand, _
                                        m_updateCommand, _
                                        m_deleteCommand, _
                                        UpdateBehavior.Standard)
            'msgbox rowsAffected

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If

        End Try

        Return rowsAffected

    End Function

    '
    ' create a record based on property values move it to the generic datatable
    ' then copy the table to the database
    '
    Public Function addCabinetRowDataSourceRecord _
            (ByVal tempDC As dc_CabinetRow, _
             Optional ByVal blnSilent As Boolean = False) _
             As Integer

        Dim intRowsAffected As Integer = 0

        Try

            initDataSetAndTable()

            Dim m_dr As DataRow = m_ds.Tables(dc_racktables.tblAttrCabinetRow.tostring).NewRow
            movePropertiesToDataRow(m_dr, tempDC)
            m_ds.Tables(dc_racktables.tblAttrCabinetRow.tostring).Rows.Add(m_dr)
            intRowsAffected = dbUpdateDataSet()

            If blnSilent = False Then
                MsgBox("DataRowsUpated = " & intRowsAffected)
            End If

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        Return intRowsAffected

    End Function


#End Region

#Region " database calls "


    '
    '
    '
    Public Sub dbSelectAllCabinetRows()

        Try

            If m_dc IsNot Nothing Then
                m_dc = Nothing
                m_dc = New dc_CabinetRow
            Else
                m_dc = New dc_CabinetRow
            End If

            initDataSetAndTable()

            ' Retrieve the initial data
            Dim cmd As DbCommand = m_db.GetStoredProcCommand(readAll)
            m_db.LoadDataSet(cmd, m_ds, dc_racktables.tblAttrCabinetRow.tostring)
            m_dt = m_ds.Tables(dc_racktables.tblAttrCabinetRow.tostring)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch Ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(Ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try



    End Sub

    '
    '
    '
    Public Sub dbSelectCabinetRowsBySublocation(ByVal sublocKey As Int32)

        Try


            initDataSetAndTable()

            ' Retrieve the initial data
            Dim cmd As DbCommand = m_db.GetStoredProcCommand("selectAttrCabinetRowsBySublocation")
            m_db.AddInParameter(cmd, "@rowLoc", DbType.Int32, sublocKey)

            m_db.LoadDataSet(cmd, m_ds, dc_racktables.tblAttrCabinetRow.tostring)

            m_dt = m_ds.Tables(dc_racktables.tblAttrCabinetRow.tostring)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch Ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(Ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try



    End Sub

    '
    '
    '
    Public Sub dbSelectCabinetRowByRowKey(ByVal rowKey As Int32)

        Try

            initDataSetAndTable()

            ' Retrieve the initial data
            Dim cmd As DbCommand = m_db.GetStoredProcCommand("selectAttrCabRowByRowKey")
            m_db.AddInParameter(cmd, "@rowKey", DbType.Int32, rowKey)

            m_db.LoadDataSet(cmd, m_ds, dc_racktables.tblAttrCabinetRow.tostring)
            m_dt = m_ds.Tables(dc_racktables.tblAttrCabinetRow.tostring)

            ' if we find the record move it to the properties
            If m_dt.Rows.Count = 1 Then
                Dim m_dr As DataRow = m_dt.Rows(0)
                moveDataRowToProperties(m_dr)
            End If

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch Ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(Ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try



    End Sub


#End Region

#Region " constructor "

    Public Sub New()

        Try

            m_db = DatabaseFactory.CreateDatabase()
            strUserId = Environment.UserDomainName & "/" & Environment.UserName

            '
            ' init an object class so that the default constants are available
            ' to the table definitions
            '

            If m_dc IsNot Nothing Then
                m_dc = Nothing
                m_dc = New dc_CabinetRow(strUserId)
            Else
                m_dc = New dc_CabinetRow(strUserId)
            End If

            initDataSetAndTable()




            buildDeleteCabinetRowCommand()
            buildInsertCabinetRowCommand()
            buildUpdateCabinetRowCommand()



        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try


    End Sub

#End Region



End Class
